module.exports = (cb, perpage) => {
    perpage = perpage || 10; //每页默认10条记录
    return (req, res, next) => { //返回中间件函数
        let page = Math.max(
            parseInt(req.params.page || '1', 10), //将参数page解析为10进制整型
            1
        ) - 1;
        cb((err, total) => { //调用传入的函数
            if (err) return next(err); //传递错误
            req.page = res.locals.page = { //保存page属性，以便后用
                number: page,
                perpage: perpage,
                from: page * perpage,
                to: page * perpage + perpage - 1,
                total: total,
                count: Math.ceil(total / perpage)
            };
            next(); //控制权交给下一个中间件
        });
    }
};